#!/bin/bash

exit_handler() {
	rm -f "${KERNEL}" "${ROOTFS}" "${OFTREE}"
}
trap 'exit_handler' 0 1 15

[ -e "${PTXDIST_BOARDSETUP}" ] || ptxd_bailout "Please run 'ptxdist boardsetup' first!"

. ${PTXDIST_BOARDSETUP}

[ -f ${PTXDIST_PLATFORMDIR}/images/linuximage ] || ptxd_bailout "Kernel-Image not found. Please run 'ptxdist images'."
KERNEL=$(mktemp ${PTXCONF_BOARDSETUP_TFTP_PATH}/kernel.XXXXXXXX)
cp -p ${PTXDIST_PLATFORMDIR}/images/linuximage ${KERNEL}

ptxd_get_path \
	"${PTXDIST_PLATFORMDIR}/images/root.jffs2" \
	"${PTXDIST_PLATFORMDIR}/images/root.ubi" ||
ptxd_bailout "Rootfs-Image not found. Please run 'ptxdist images'."

ROOTFS="$(mktemp ${PTXCONF_BOARDSETUP_TFTP_PATH}/rootfs.XXXXXXXX)"
cp -p "${ptxd_reply}" "${ROOTFS}"

if [ -f "${PTXDIST_PLATFORMDIR}/images/oftree" ]; then
	OFTREE="$(mktemp ${PTXCONF_BOARDSETUP_TFTP_PATH}/oftree.XXXXXXXX)"
	cp -p "${PTXDIST_PLATFORMDIR}/images/oftree" "${OFTREE}"
fi

${PTXDIST_TOPDIR}/tests/kwrapper flash "${KERNEL}" "${ROOTFS}" "${OFTREE}"

